/**
    @project {
        "templateId": "org.helgoboss.custom",
        "version": "0.1-SNAPSHOT",
        "dependencies": [
            "org.scala-lang:scala-library:2.9.0-1",
            "org.helgoboss:osgi-additions:0.1-SNAPSHOT",
            "org.helgoboss:commandlet-http-provider:0.1-SNAPSHOT"
        ]
    }
*/
package org.helgoboss.commandlet_http_provider_config_writer

import org.helgoboss.osgi_additions.{OsgiContext, OsgiServiceWatcher}
import org.helgoboss.commandlet_http_provider.CommandletHttpProvider
import java.util.Properties
import java.io.{File, FileInputStream, FileOutputStream}


object Activator {
    val ConfigFile = new File(System.getProperty("user.home") + "/.commandlet-platform/config.properties")
}

class Activator extends OsgiContext with OsgiServiceWatcher {
    import Activator._
    
    whenBundleActive {
        whenServicePresent[CommandletHttpProvider] { httpProvider =>
            onStart {
                val properties = new Properties
                if (ConfigFile.exists) {
                    properties.load(new FileInputStream(ConfigFile))
                }
                properties.setProperty("port", httpProvider.port.toString)
                properties.setProperty("context_path", httpProvider.contextPath)
                ConfigFile.getParentFile.mkdirs
                properties.store(new FileOutputStream(ConfigFile), "")
            }
        }
    }
}



